/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.logging;

import java.awt.Color;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
import javax.swing.JComponent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;

/**
 * Controller of the log panel.
 * 
 * @author Timon Veenstra
 */
public final class LogController extends Handler {

    private final LogPanel panel = new LogPanel();
    private static final LogController instance = new LogController();

    public static LogController getInstance() {
        return instance;
    }

    private LogController() {
        setLevel(Level.INFO);
        setFormatter(new SimpleFormatter());
    }

    public JComponent getPanel() {
        return this.panel;
    }

    @Override
    public void publish(LogRecord record) {
        if (isLoggable(record)) {
            StyledDocument doc = panel.getTextPane().getStyledDocument();

            try {

                if(record.getLevel().equals(Level.WARNING)){
                    Style warningStyle = doc.addStyle("Warning", null);
                    StyleConstants.setForeground(warningStyle, new Color(240, 120, 0)); // Orange
                    StyleConstants.setBold(warningStyle, true);
                    doc.insertString(doc.getLength(), "[" + NbBundle.getMessage(LogController.class, "message.logging.warning") + "] " + getFormatter().formatMessage(record), warningStyle);
                }
                else if(record.getLevel().equals(Level.SEVERE))
                {
                    Style errorStyle = doc.addStyle("Error", null);
                    StyleConstants.setForeground(errorStyle, new Color(237, 28, 36)); // Red
                    StyleConstants.setBold(errorStyle, true);
                    doc.insertString(doc.getLength(), "[" + NbBundle.getMessage(LogController.class, "message.logging.error") + "] " + getFormatter().formatMessage(record), errorStyle);
                }
                else {
                    Style succeededStyle = doc.addStyle("Succeeded", null);
                    StyleConstants.setForeground(succeededStyle, new Color(1, 10, 171)); // Blue
                    doc.insertString(doc.getLength(), "[" + NbBundle.getMessage(LogController.class, "message.logging.succeeded") + "] " + getFormatter().formatMessage(record), succeededStyle);
                }
                doc.insertString(doc.getLength(), "\n" , null);
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    @Override
    public void flush() {

    }

    @Override
    public void close() throws SecurityException {
    }
}
